package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.validator.constraints.impl.EmailValidator;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import validator.RegistrationValidator;

import beans.User;

import dao.UserDAO;

public class UserRegisterController extends MultiActionController {

	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	private RegistrationValidator registrationValidator;
	
	public void setRegistrationValidator(RegistrationValidator registrationValidator) {
		this.registrationValidator = registrationValidator;
	}
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		return registrationValidator.validate(user);
			}
	
	public ModelAndView register(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			ModelMap modelMap = new ModelMap();
			modelMap.addAttribute("user", new User());
			return new ModelAndView("register", modelMap);
	}
 
	public ModelAndView success(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
			return new ModelAndView("success",modelMap);
	}

	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
			return new ModelAndView("login", modelMap);
	}
	
	public ModelAndView accessDenied(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
			return new ModelAndView("accessDenied",modelMap);
	}
	public static boolean validateEmailAddress(String sEmail){
		EmailValidator emailValidator = new EmailValidator();
		return emailValidator.isValid(sEmail, null);
		}
}
